<?php
class Model_Tips extends Zend_Db_Table_Abstract
{
    protected $_name = 'tips';

    public function getPaginator($filters = array(), $sort = null, $page = 1, $rowCount = null)
    {
        // get select query
        $select = $this->select ();
        if ($filters != null)
        {
            if (is_array ( $filters ))
            {
                foreach ( $filters as $key => $val )
                    $select->where ( $key, $val );
            }
            else
            {
                $select->where ( $filters );
            }
        }
        
        if ($sort != null)
            $select->order ( $sort );
            
        // get adapter
        $adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
        
        // get paginator
        $paginator = new Zend_Paginator ( $adapter );
        $paginator->setItemCountPerPage ( $rowCount );
        $paginator->setCurrentPageNumber ( $page );
        
        return $paginator;
    }

    public function getList($filters = array(), $sort = null, $page = null, $rowCount = null)
    { 
        $select = $this->select ();
        if ($filters != null)
        {
            if (is_array ( $filters ))
            {
                foreach ( $filters as $key => $val )
                    $select->where ( $key, $val );
            }
            else
            {
                $select->where ( $filters );
            }
        }
        
        if ($page != null)
            $select->limitPage ( $page, $rowCount );
        
        if ($sort != null)
            $select->order ( $sort );
            
        return $this->fetchAll ( $select );
    }
    
    public function getTipRecords() 
    {
    	$query = $this->getAdapter()->query("SELECT COUNT(*) as total, YEAR(`date`) AS intervalYear, MONTH(`date`) AS intervalMonth, result FROM tips GROUP BY YEAR(`date`), MONTH(`date`), result ORDER BY `date` DESC");
    	$records = $query->fetchAll();
    	
    	$tipRecords = array();
    	foreach($records as $record) 
    	{
    		$tipRecords[$record["intervalYear"]][$record["intervalMonth"]][$record["result"]] = $record["total"];
    	}
    	return $tipRecords;
    }

    public function createTip($match, $tip, $date, $type, $result)
    {
        // create a new row in the table
        $row = $this->createRow ();
       
        // set the row data
        $row->match = $match;
        $row->tip = $tip;
        $row->date = date_create_from_format( 'd-m-Y', $date )->format('Y-m-d');
        $row->type = 1;
        $row->result = ($result == "" ? NULL : $result) ;
        $dateObject = Zend_Date::now ();
        $row->created_ts = $dateObject->toString("YYYY-MM-dd HH:mm:ss");
        
        // save the new row
        $row->save ();
        
        // now fetch the id of the row you just created and return it 
        $id = $row->id; //$this->_db->lastInsertId();
        return $id;
    }
    
    public function updateTip($id, $match, $tip, $date, $type, $result)
    {
        // find the row that matches the id
        $row = $this->find ( $id )->current ();
        if ($row)
        {
            // set the row data
            $row->match = $match;
	        $row->tip = $tip;
	       	$row->date = date_create_from_format( 'd-m-Y', $date )->format('Y-m-d');
	        $row->type = $type;
	        if($result) {
	        	$row->result = $result;
	        }
	        
            // save the new row
            $row->save ();
            return true;
        }
        else
        {
            throw new Zend_Exception ( "Update function failed; could not find row!" );
        }
    }

    public function deleteTip($id)
    {
        // find the row that matches the id
        $row = $this->find ( $id )->current ();
        if ($row)
        {
            $row->delete ();
            return true;
        }
        else
        {
            throw new Zend_Exception ( "Delete function failed; could not find row!" );
        }
    }
}
?>
